package org.devgframwork.demo.es5.controller;

import org.devgframwork.demo.es5.entity.CmsDocument;
import org.devgframwork.es5.elasticserach.EsResult;
import org.devgframwork.es5.elasticserach.EsSearch;
import org.devgframwork.platform.model.ResultModel;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/test/es5")
public class TestEs5Controller {

    @RequestMapping("getCmsDocumentData")
    public ResultModel<EsResult<CmsDocument>> getCmsDocumentData() throws Exception {
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
        // 过滤客户类型
        BoolQueryBuilder custBuilder = QueryBuilders.boolQuery();
        QueryBuilder defaultCustTypeBuilder = QueryBuilders.termQuery("cust_type_array.keyword", "-1");
        custBuilder.should(defaultCustTypeBuilder);
        QueryBuilder custTypeBuilder = QueryBuilders.termQuery("cust_type_array.keyword", "3");
        custBuilder.should(custTypeBuilder);
        queryBuilder.must(custBuilder);
        // 过滤客户区域
        BoolQueryBuilder areaBuilder = QueryBuilders.boolQuery();
        QueryBuilder defaultAreaCodeBuilder = QueryBuilders.termQuery("area_code_array.keyword", "-1");
        areaBuilder.should(defaultAreaCodeBuilder);
        QueryBuilder areaCodeBuilder = QueryBuilders.termQuery("area_code_array.keyword", "100083");
        areaBuilder.should(areaCodeBuilder);
        queryBuilder.must(areaBuilder);
        searchSourceBuilder.query(queryBuilder);
        EsResult<CmsDocument> esResult = EsSearch.getEsResult(searchSourceBuilder, 1, 10, "cms", "document",CmsDocument.class);
        return ResultModel.newSuccess(esResult);
    }
}
